// 页面加载完毕后执行
document.addEventListener('DOMContentLoaded', function () {
	const provinceSelect = document.querySelector('#provinceSelect');
	const citySelect = document.querySelector('#citySelect');
	const schoolSelect = document.querySelector('#schoolSelect');

	// 向省会下拉菜单填充数据
	const idArr = Object.keys(province);
	let addHtml = '';
	idArr.forEach((id) => {
		// 拼接所有option的html
		const op = `<option value="${id}">${province[id]}</option>`;
		addHtml += op;
	});
	// console.log(addHtml);
	// 添加option
	provinceSelect.innerHTML += addHtml;
	addHtml = ''; // 作为后面拼接option的间接变量

	provinceSelect.addEventListener('change', function () {
		const cities = this.value === '0000' ? '' : city[this.value];
		const cityArr = cities && Object.entries(cities);

		if (cityArr) {
			citySelect.innerHTML = '<option value="0000">请选择</option>';
      schoolSelect.innerHTML = '';
			cityArr.forEach((ci) => {
				addHtml += `<option value="${ci[0]}">${ci[1]}</option>`;
			});
			citySelect.innerHTML += addHtml;
			addHtml = '';
		}
	});

	citySelect.addEventListener('change', function () {
		const schoolArr = this.value === '0000' ? '' : allschool[this.value];
		if (schoolArr) {
			schoolSelect.innerHTML = '<option>请选择</option>';
			schoolArr.forEach((sc) => {
				addHtml += `'<option>${sc}</option>'`;
			});
			schoolSelect.innerHTML += addHtml;
			addHtml = '';
		}
	});
});
